/**
 * @Author: easten
 * @Created: 2024-07-27 17:30:26
 * @Description: 物联网数据服务中心
 *
 */
import { IotDeviceData } from "_common/DataType";
import { BehaviorSubject, Observable } from "rxjs";

class IotDataService {
  private dataSubject: BehaviorSubject<IotDeviceData[]> = new BehaviorSubject<
    IotDeviceData[]
  >([]);

  private iotSubject: BehaviorSubject<IotDeviceData> =
    new BehaviorSubject<IotDeviceData>({});

  public data$: Observable<IotDeviceData[]> = this.dataSubject.asObservable();
  public iot: Observable<IotDeviceData> = this.iotSubject.asObservable();
  /**
   * 设置数据并通过主题发布新的数据
   *
   * @param data - 要设置的数据，可以是 IotDeviceData 类型或 null
   */
  public setData(data: IotDeviceData[]): void {
    this.dataSubject.next(data);
  }

  public upData(data: IotDeviceData): void {
    this.iotSubject.next(data);
    /* const currentData = this.dataSubject.getValue();
    const updatedData = currentData.map((data) =>
      data.deviceCode === newData.deviceCode ? newData : data
    );
    // 如果 newData.id 不在 currentData 中，添加新数据
    if (!currentData.some((data) => data.deviceCode === newData.deviceCode)) {
      updatedData.push(newData);
    }
    this.dataSubject.next(updatedData); */
  }
}

const iotDataService = new IotDataService();
export default iotDataService;
